<template>
  <div class="login_container">
    <el-row>
      <el-col :span="12" :xs="0" style="color: brown">我是占位区</el-col>
      <el-col :span="12" :xs="24">
        <el-form
          class="login_form"
          :model="loginForm"
          :rules="loginFormRules"
          ref="loginFormRef"
        >
          <h1>Hello</h1>
          <h2>欢迎来到动感地带</h2>
          <el-form-item prop="phone">
            <el-input
              :prefix-icon="User"
              placeholder="账号"
              v-model="loginForm.phone"
            ></el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input
              :prefix-icon="Lock"
              type="password"
              placeholder="密码"
              v-model="loginForm.password"
              show-password
            ></el-input>
          </el-form-item>
          <el-form-item>
            <el-button
              :loading="loading"
              type="primary"
              class="login_button"
              @click="login(loginFormRef)"
              >登录</el-button
            >
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>
  </div>
</template>

<script setup lang="ts">
import { User, Lock } from '@element-plus/icons-vue'
import { reactive, ref } from 'vue'
import useUserStore from '@/store/modules/user'
import { useRoute, useRouter } from 'vue-router'
import { ElNotification } from 'element-plus'
import { getTimeDesc } from '@/utils/time'
import type { FormInstance, FormRules } from 'element-plus'
import type { LoginFormData } from '@/api/user/type'

let loading = ref(false)

const loginForm = reactive<LoginFormData>({
  phone: '15712345678', // 账号、密码给个默认值
  password: '123456',
})

let loginFormRef = ref<FormInstance>()

const userStore = useUserStore()
const router = useRouter()
const route = useRoute()

// 自定义表单校验规则函数
const checkPassword = (rule: any, value: string, callback: any) => {
  if (value === '' || value.length !== 6) {
    callback(new Error('密码必须是六位'))
  } else {
    callback()
  }
}

const loginFormRules = reactive<FormRules<LoginFormData>>({
  phone: [{ min: 3, max: 11, message: '账号必须是3到11位', trigger: 'blur' }],
  // password: [{ len: 6, message: '密码必须是六位', trigger: 'blur' }],
  password: [{ validator: checkPassword, trigger: 'blur' }],
})

let login = async (formEl: FormInstance | undefined) => {
  if (!formEl) return
  await formEl.validate(async (valid, fields) => {
    if (valid) {
      try {
        loading.value = true
        await userStore.userLogin(loginForm)
        await userStore.getUserInfo(loginForm.phone)
        // any 可以赋值给任何类型的变量
        // 如果请求query参数中包含 redirect 参数，则跳到 redirect 对应的路径中去
        let redirect: any = route.query.redirect
        router.push({ path: redirect || '/' })
        ElNotification({
          type: 'success',
          message: '登录成功',
          title: `HI，${getTimeDesc()}好`,
        })
      } catch (e) {
        // ElNotification({ type: 'error', message: (e as Error).message })
      } finally {
        loading.value = false
      }
    }
  })
}
</script>

<style scoped>
.login_container {
  width: 100%;
  height: 100vh;
  background: url('@/assets/images/background.jpg') no-repeat;
  background-size: cover;
  .login_form {
    position: relative;
    width: 50%;
    top: 30vh;
    background-color: rgba(124, 230, 100, 0.5); /* 红色背景，50% 透明度 */
    padding: 10%;
    h1 {
      font-size: 40px;
      color: white;
    }
    h2 {
      font-size: 20px;
      color: white;
    }
    .login_button {
      width: 100%;
    }
  }
}
</style>
